# 利用数值微分求出的梯度结果
# 来确认误差反向传播法的实现是否正确

import sys,os
sys.path.append(os.pardir)
import numpy as np 
from dataset.mnist import load_mnist
from two_layer_net import TwoLayerNet

# 读入数据
(x_train,t_train),(x_test,t_test) = load_mnist(normalize=True,one_hot_label=True)
# 初始化网络
network = TwoLayerNet(input_size=784,hidden_size=50,output_size=10)

# 缩小训练数据
x_batch = x_train[:3]
t_batch = t_train[:3]

# 求梯度
grad_numerical = network.numerical_gradient(x_batch,t_batch)
grad_backprop = network.gradient(x_batch,t_batch)

for key in grad_numerical.keys():
    diff = np.average(np.abs(grad_numerical[key]-grad_backprop[key]))
    print(key+" : "+ str(diff))

